package com.ruiyi.careryun.service.ota.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruiyi.careryun.beans.VersionMap;
import com.ruiyi.careryun.mapper.ota.ServerConfigInfoMapper;
import com.ruiyi.careryun.mapper.ota.VersionInfoMapper;
import com.ruiyi.careryun.service.ota.VersionInfoService;

@Service
public class VersionInfoServiceImpl implements VersionInfoService {

    @Autowired
    private VersionInfoMapper versionInfoMapper;
    @Autowired
    private ServerConfigInfoMapper serverConfigInfoMapper;

    @Override
    public List<String> getOldVersion(String string) {
        return versionInfoMapper.getOldVersion(string);
    }

    @Override
    public Long updateURL(String tableName, String fileURL, String oldVersion, String fileMd5) {
        return versionInfoMapper.updateURL(tableName, fileURL, oldVersion, fileMd5);
    }

    @Override
    public Long insertOldVersion(String tableName, String oldVersion) {
        return versionInfoMapper.insertOldVersion(tableName, oldVersion);
    }

    @Override
    public Boolean insertOldVersionAndUpdateNewVersion(String tableName, String oldVersion, String configName, String newVersion) {
        Long num1 = versionInfoMapper.insertOldVersion(tableName, oldVersion);
        Long num2 = serverConfigInfoMapper.updateNewVersion(configName, newVersion);
        if (num1 != null && num1 > 0 && num2 != null && num2 > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateNewVersion(String configName, String newVersion) {
        Long num = serverConfigInfoMapper.updateNewVersion(configName, newVersion);
        if (num != null && num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public VersionMap getUpdateInfo(String version) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("oldVersion", version);
        return versionInfoMapper.getUpdateInfo(params);
    }

}
